package edu.westga.cs3152.project2;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Hashtable;
import java.util.Scanner;

/**
 * The Dictionary reads in a file of and keeps track of a list of valid English
 * words. It also provides methods to access this list and see if words are
 * contained within the dictionary.
 * 
 * @author Lewis Williams
 * @version 2011.3.18
 */
public class Dictionary {
	
	//The Dictionary Hashtable
	private Hashtable<Integer, String> dictionary = new Hashtable<Integer, String>();
	
	/**
	 * A default constructor for Dictionary that does nothing.
	 */
	public Dictionary(){}

	/**
	 * A constructor for Dictionary that accepts the file path for a source file for 
	 * the words and calls populateDictionary to add these words to the hash table.
	 * @param sourceFile
	 * 			The file path of the file containing the words to be added to the dictionary.
	 */
	public Dictionary(String sourceFile){
		populateDictionary(sourceFile);
	}

	/**
	 * Populates the dictionary hashtable by reading in a file containing a list of words.
	 * 
	 * @param sourceFile
	 * 			The file containing the words to be added to the dictionary.
	 */
	private void populateDictionary(String sourceFile) {
		try{
    		//Creates the file
    		File words = new File(sourceFile);
    		//Creates the scanner to read the file.
    		Scanner scan = new Scanner(words);
    		//The word currently being read in from the file
    		String currentWord = "";
    		//Populates hashtable by reading in and parsing the lines of the file.
    		while(scan.hasNext()){
    			currentWord = scan.nextLine();
    			dictionary.put(currentWord.hashCode(), currentWord);
    		}
    	}
    	//Catches FileNotFoundException if the board file cannot be found.
    	catch(FileNotFoundException fnfe){
    		System.out.println("The dictionary file cannot be found");
    	}				
	}
	
	public boolean contains(String word){
		return dictionary.containsKey(word.hashCode());
	}
	
}
